﻿/*!
 * 框架控件.
 * ecp.qzz.ui.frame.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.panel","qzz.scrollbar"], function() {	
	
	var qzz = window.qzz;
	qzz.space("ui");
	/**
	 * 框架控件.
	 */
	qzz.ui.frame = qzz.Class(qzz.ui.panel, {
		/**
		 * 创建.
		 */
	    _create:function() {
	    	this.base();
            this.className = "Frame";
            //this.logInfo("Frame.create");
		    //滚动条初始化
		    this._hScrollHeight = 18;
		    this._pageBoxHeight = 0;
		    this._vScrollHide = false;
		    this._hScrollHide = true;
		    //宽度差
		    this.maxer = 2;
	    },
	    /**
	     * 渲染.
	     */
	    _render:function() {
	    	this.base();
	    	//滚动条.
            this.scrollBar = new qzz.ui.scrollbar(undefined, {"cssPrefix":this._cssPrefix}); 
		    //滚动事件.
            var _this = this;
		    this.scrollBar.onScroll = function(scrollIndex, scrollCount) {
	    	    _this.doScroll(scrollIndex, scrollCount);
	    	}
	    	//创建主容器
	    	this.frameTable = this.createElement("table");
	    	this.thisWindow.appendChild(this.frameTable);
	    	this.setStyle(this.frameTable, "component_ui_listview_table");
	    	this.frameTable.style.width = this._getRectWidth() + "px";
	    	if(this._getRectHeight() > 0) {
	    	    this.frameTable.style.height = this._getRectHeight() + "px";
	    	}
	    	var tr = this.frameTable.insertRow(0);
	    	this.mainbox = tr.insertCell(0);
	    	this.setStyle(this.mainbox, "component_ui_listview_box_td");
	    	var mbw = this._getRectWidth() - this.scrollBar.getWidth();
	    	if(mbw <= 0) {
	    		mbw = 1;
	    	}
	    	this.mainbox.style.width = mbw + "px";
	    	//添加滚动条
	    	this.scrollbox = tr.insertCell(1);
	    	this.scrollbox.style.width = this.scrollBar.getWidth() + "px";
	    	this.scrollBar.setHeight(this._getRectHeight() - this._hScrollHeight - this._pageBoxHeight);
	    	this.setStyle(this.scrollbox, "component_ui_listview_box_td");
	    	this.scrollBar.setParent(this.scrollbox);
	    	//列表容器
	    	this.listDiv = this.createElement("div");	
	    	var mbh = this._getRectHeight() - this._pageBoxHeight;
	    	if(mbh <= 0) {
	    		mbh = 1;
	    	}
	    	this.listDiv.style.height = mbh + "px";
	    	
	    	var mbw = this._getRectWidth() - this.scrollBar.getWidth() - 2;
	    	if(mbw <= 0) {
	    		mbw = 1;
	    	}
	    	this.listDiv.style.width = (mbw + this.maxer) + "px";
	    	this.setStyle(this.listDiv, "treeListDiv");
	    	this.mainbox.appendChild(this.listDiv);
	    	//移动内部的控件
	    	var len = this.thisWindow.childNodes.length;
	    	if(len > 1) {
	    		var cnAry = [];
	    		for(var i = 0; i < len; i++) {
	    		    var cn = this.thisWindow.childNodes[i];
	    		    if(cn !== this.frameTable) {
	    		    	cnAry.push(cn);
	    		    }
	    		    cn = null;
	    		}
	    		for(var i = 0, len = cnAry.length; i < len; i++) {
	    			this.listDiv.appendChild(cnAry[i]);
	    		}
	    		cnAry = null;
	    	}
	    },
	    /**
	     * 设置大小.
	     */
	    _doResize:function(el) {
	    	if(!this._update) return false;
	    	this.base(el);	
	    	this.logBegin("Frame doRisize")
	    	var rh = this._getRectHeight();
	    	var rhpx = rh + "px";
    		var rw = this._getRectWidth();
    		var rwpx = rw + "px";
	    	if(this.isNotEmpty(this.listDiv) && rh > 0 && rw > 0) {
	    		if(this.listDiv.style.height != (rh - this._pageBoxHeight) + "px") {
	    	        this.listDiv.style.height = (rh - this._pageBoxHeight) + "px";
	    		}
	    		this.scrollBar.setHeight(rh - this._pageBoxHeight);
	    		this.scrollBar.refresh();
	    		if(this.frameTable.style.width != rwpx) {
	    	        this.frameTable.style.width = rwpx;
	    		}
	    		if(this.frameTable.style.height != rhpx) {
	    	        this.frameTable.style.height = rhpx;
	    		}
	    		if(!this.scrollBar.visible) {
	    	    	rwpx = (rw - 1) + "px";
	    	    	if(this.mainbox.style.width != rwpx) {
	    	    	     this.mainbox.style.width = rwpx;
	    	    	}
	    	    	if(this.scrollbox.style.width != "1px") {
			    	    this.scrollbox.style.width = "1px";
	    	    	}
	    	    	var mbw = (this._getNumberValue(this.mainbox.style.width, 1) + this.maxer) + "px";
	    	    	if(this.listDiv.style.width != mbw) {
			    	    this.listDiv.style.width = mbw;
	    	    	}
		    	} else {
		    		var srcW = this.scrollBar.getWidth();
		    		rwpx = (rw - srcW) + "px";
		    		if(this.mainbox.style.width != rwpx) {
		    		    this.mainbox.style.width = rwpx;
		    		}
		    		rwpx = srcW + "px";
		    		if(this.scrollbox.style.width != rwpx) {
			    	    this.scrollbox.style.width = rwpx;
		    		}
		    		rwpx = (rw - srcW - 1 + this.maxer) + "px";
		    		if(this.listDiv.style.width != rwpx) {
			    	    this.listDiv.style.width = rwpx;
		    		}
		    	}
	    	}
	    	this.logEnd("Frame doResize(" + this.getOption().top + "," + this.getOption().left + "," + this.getOption().width + "," + this.getOption().height + ")");
	    },
	    /**
	     * 滚动事件.
	     * @param scrollIndex 滚动序号
	     * @param scrollCount 滚动数量
	     */
        doScroll:function(scrollIndex, scrollCount) {
	    	
	    },
	    /**
	     * 设置滚动条隐藏.
	     * @param hide 状态
	     */
	    setVScrollHide:function(hide) {
	    	this._vScrollHide = hide;
	    	this.scrollBar.setVisible(!hide);
	    	if(hide) {
		    	this.mainbox.style.width = (this._getRectWidth() - 1) + "px";
		    	this.scrollbox.style.width = "1px";
		    	this.listDiv.style.width = (this._getRectWidth() - 1 + this.maxer) + "px";
	    	} else {
	    		this.mainbox.style.width = (this._getRectWidth() - this.scrollBar.getWidth()) + "px";
		    	this.scrollbox.style.width = this.scrollBar.getWidth() + "px";
		    	this.listDiv.style.width = (this._getRectWidth() - this.scrollBar.getWidth() - 1 + this.maxer) + "px";
	    	}
	    },
	    /**
	     * 获取滚动条高度.
	     * @param 返回
	     */
	    getHScrollHeight:function() {
	    	return this._hScrollHeight;
	    },	  
	    /**
	     * 获取滚动条是否隐藏.
	     * @return 返回
	     */
	    getHScrollHide:function() {
	        //添加空判断
	    	return this.isEmpty(this.listDiv) || (this.listDiv.offsetHeight - this.listDiv.scrollHeight <= 10);
	    },
	    /**
	     * 重新设置滚动条高度.
	     */
	    refreshScrollBar:function() {
	    	//重设置滚动条高度
		    if(this.getHScrollHide()) {
		    	this.scrollBar.setHeight(this._getRectHeight() - 2 - this._pageBoxHeight);
		    } else {
		    	this.scrollBar.setHeight(this._getRectHeight() - this._hScrollHeight - this._pageBoxHeight);
		    }
		    this.setVScrollHide(this.scrollBar.getVisible() !== true);
	    },
	    /**
	     * 获取容器窗口的宽度.
	     */
	    getBoxWidth:function() {
	    	return this._getNumberValue(this.listDiv.style.width, this.getWidth());
	    },
	    /**
	     * 有些浏览器横向滚动条不会自动刷新.
	     */
	    refreshVScrollBar:function() {
	    	var me = this;
	    	this.listDiv.style.overflow = "hidden";
	    	this._asynRun(function() {
	    		me.listDiv.style.overflow = "";
	    	}, 0)
	    }
	});
	
	_bindRenderFunc("qzzframe", qzz.ui.frame);
	return qzz.ui.frame;
});